/**
 * KeepAliveCatalog.java
 * @author Daniel Liu
 * @version 2006-12-22 ����12:31:34
 */
package org.lex.ftp.ui.prefs;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;

import org.lex.app.prefs.ResourceMap;
import org.lex.awt.ComponentUtilities;
import org.lex.ftp.FTP;
import org.lex.ftp.util.prefs.Keys;
import org.lex.swing.prefs.CategoryAdapter;


public class KeepAliveCategory extends CategoryAdapter {
	private static final String[] COMMAND = { "NOOP", "PWD", "CWD .", "LIST" };

	private JCheckBox enableKeepAliveCheckBox = null;
	private JLabel keepAliveCommandLabel = null;
	private JComboBox keepAliveCommandComboBox = null;
	private JLabel keepAliveDelayLabel = null;
	private JTextField keepAliveDelayTextField = null;
	private JPanel gluePane = null;

	public KeepAliveCategory() {
		super();
		this.initialize();

	}

	public Component getComponent() {
		return this;
	}

	public String getTitle() {
		return "Net.Keep Alive";
	}

	public void loadConfiguration() {
		ResourceMap config = FTP.getContext().getResourceMap();
		boolean enabled = config.getBoolean(Keys.KEEP_ALIVE_ENABLED);
		String cmd = config.getString(Keys.KEEP_ALIVE_COMMAND);
		String delay = config.getString(Keys.KEEP_ALIVE_DELAY);
		this.getEnableKeepAliveCheckBox().setSelected(enabled);
		this.getKeepAliveCommandComboBox().setSelectedItem(cmd);
		this.getKeepAliveDelayTextField().setText(delay);
	}

	public void saveConfiguration() {
		ResourceMap config = FTP.getContext().getResourceMap();
		boolean enabled = this.getEnableKeepAliveCheckBox().isSelected();
		String cmd = getKeepAliveCommandComboBox().getSelectedItem().toString();
		String delay = this.getKeepAliveDelayTextField().getText().trim();
		config.putProperty(Keys.KEEP_ALIVE_ENABLED, Boolean.valueOf(enabled));
		config.putProperty(Keys.KEEP_ALIVE_COMMAND, cmd);
		config.putProperty(Keys.KEEP_ALIVE_DELAY, delay);
	}

	// =======================
	private void initialize() {
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.gridy = 3;
		GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
		gridBagConstraints5.fill = GridBagConstraints.NONE;
		gridBagConstraints5.gridy = 2;
		gridBagConstraints5.weightx = 0.0;
		gridBagConstraints5.anchor = GridBagConstraints.WEST;
		gridBagConstraints5.insets = new Insets(0, 6, 5, 5);
		gridBagConstraints5.ipadx = 100;
		gridBagConstraints5.gridx = 1;
		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		gridBagConstraints4.gridx = 0;
		gridBagConstraints4.anchor = GridBagConstraints.WEST;
		gridBagConstraints4.insets = new Insets(0, 22, 5, 0);
		gridBagConstraints4.gridy = 2;
		keepAliveDelayLabel = new JLabel();
		keepAliveDelayLabel.setText("Keep Alive Delay( sec. )");
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.fill = GridBagConstraints.NONE;
		gridBagConstraints3.gridy = 1;
		gridBagConstraints3.weightx = 0.0;
		gridBagConstraints3.anchor = GridBagConstraints.WEST;
		gridBagConstraints3.insets = new Insets(0, 6, 5, 5);
		gridBagConstraints3.ipadx = 73;
		gridBagConstraints3.gridx = 1;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.anchor = GridBagConstraints.WEST;
		gridBagConstraints2.insets = new Insets(0, 22, 5, 0);
		gridBagConstraints2.gridy = 1;
		keepAliveCommandLabel = new JLabel();
		keepAliveCommandLabel.setText("Keep Alive Command");
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 0;
		gridBagConstraints1.gridwidth = 2;
		gridBagConstraints1.anchor = GridBagConstraints.WEST;
		gridBagConstraints1.insets = new Insets(0, 0, 5, 0);
		gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints1.gridy = 0;
		// ==================================
		this.setLayout(new GridBagLayout());
		this.setBorder(BorderFactory.createTitledBorder(null, "Keep Alive Settings", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		this.add(getEnableKeepAliveCheckBox(), gridBagConstraints1);
		this.add(keepAliveCommandLabel, gridBagConstraints2);
		this.add(getKeepAliveCommandComboBox(), gridBagConstraints3);
		this.add(keepAliveDelayLabel, gridBagConstraints4);
		this.add(getKeepAliveDelayTextField(), gridBagConstraints5);
		this.add(getGluePane(), gridBagConstraints);
	}

	private JCheckBox getEnableKeepAliveCheckBox() {
		if (enableKeepAliveCheckBox == null) {
			enableKeepAliveCheckBox = new JCheckBox();
			enableKeepAliveCheckBox.setText("Enable the Keep Alive System");
			enableKeepAliveCheckBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean selected = enableKeepAliveCheckBox.isSelected();
					ComponentUtilities.setEnabled(selected, getKeepAliveCommandComboBox(), getKeepAliveDelayTextField(), keepAliveCommandLabel,
							keepAliveDelayLabel);
				}
			});
		}
		return enableKeepAliveCheckBox;
	}

	private JComboBox getKeepAliveCommandComboBox() {
		if (keepAliveCommandComboBox == null) {
			keepAliveCommandComboBox = new JComboBox();
			for (String cmd : COMMAND)
				keepAliveCommandComboBox.addItem(cmd);
		}
		return keepAliveCommandComboBox;
	}

	private JTextField getKeepAliveDelayTextField() {
		if (keepAliveDelayTextField == null) {
			keepAliveDelayTextField = new JTextField();
		}
		return keepAliveDelayTextField;
	}

	/**
	 * This method initializes gluePane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getGluePane() {
		if (gluePane == null) {
			gluePane = new JPanel();
			gluePane.setLayout(new GridBagLayout());
		}
		return gluePane;
	}
}
